﻿angular.module("umbraco").controller("Gary.OnlineConsultingDashboardController", function ($scope, $window, consultResource) {
    var vm = this;
    var isDebug = false;

    consultResource.getConsultType().then(function (response) {
        if (isDebug) {
            console.log(response);
        }
        vm.consultTypes = response;
    });

    $scope.status = function (s) {
        return s == "n" ? "未处理" : "已处理";
    }

    $scope.convertType = function (i) {
        result = "没有匹配到" + i;
        angular.forEach(vm.consultTypes, function (item) {
            if (parseInt(item.nid) == parseInt(i)) {
                result = item.name;
            }
        });
        return result;
    }

    
    var search = function () {
        consultResource.getConsults(1, "aa").then(function (response) {
            if (isDebug) {
                console.log(response);
            }
            vm.consults = response;
        });
    }

    search();

    

    $scope.import = function (id) {
        if (isDebug) {
            console.log("导入信息" + id);
        }
        consultResource.importConsults(id).then(function (response) {
            if (isDebug) {
                console.log(response);
            }
            if (parseInt(response.errCode) == 0) {
                if (isDebug) {
                    console.log(response);
                    console.log($window.location.href);
                }
                $window.location.href = "/umbraco/#/content/content/edit/" + response.errMessage;
            } else {
                alert(response.errMessage);
            }
        });
    }
    $scope.delete = function (id) {
        if (isDebug) {
            console.log("删除信息" + id);
        }
        consultResource.deleteConsults(id).then(function (response) {
            if (isDebug) {
                console.log(response);
            }
            if (parseInt(response.errCode) == 0) {
                search();
            } else {
                alert(response.errMessage);
            }
        });
    }

    $scope.doSearch = function () {
        if (isDebug) {
            console.log("点击搜索了");
            /*
            both is ok
            console.log(angular.element("#selConsultType").val());
            console.log($scope.txtKeyword);
            */
        }
        search();
    }
});